package flex.cc.store.quartz;

import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import flex.cc.store.dto.entity.StoreCouponEntity;
import flex.cc.store.dto.entity.StorePackageEntity;
import flex.cc.store.service.StoreCouponService;
import flex.cc.store.service.StorePackageService;
import flex.cc.util.DateUtil;

/**
 * 
 * @author WANGCONG
 * 2021年12月23日上午9:28:06
 * @desc 定时任务类：每晚0时执行，更新过期服务包、卡券状态
 */
@Component
@EnableScheduling
public class QuartzTask {

	@Resource
	private StoreCouponService couponService;
	@Resource
	private StorePackageService packageService;
	
	/**
	 * 
	 * @author WANGCONG
	 * 2021年12月23日上午9:30:12
	 * @desc 状态变更规则：
	 * 1.服务包不管用没用，到了失效时间就变为已失效，卡券是到了过期时间，没使用的卡券变为过期；
	 * 2.包过期的话，里面的所有的没使用的卡券也过期
	 * 
	 * 状态：
	 * 服务包：0-未兑换 1-已兑换 2-已失效  3-已取消；
	 * 卡券：     0-未生效、1-有效、2-已过期、3-已使用（核销完成）、4-已取消
	 */
	//每天5：13执行任务输出“我是定时任务
//	@Scheduled(cron = "0 17 05 * * ?")
//	@Scheduled(cron = "*/10 * * * * ?") //10秒执行一次
//	@Scheduled(cron = "0 31 10 * * ?")    //每天十点半
	@Scheduled(cron = "0 0 0 * * ?")    //每天凌晨零时执行
    public void changeStatus(){
        System.out.println("我是定时任务");
        Map<String, Object> search_package_params = new HashMap<String, Object>();
        Date now = new Date();
        search_package_params.put("expDate", now);
        search_package_params.put("status", "2");
		List<StorePackageEntity> packageList = packageService.findListByParam(search_package_params);
        for(int i=0; i<packageList.size(); i++){
        	Date executeTime = new Date();
        	StorePackageEntity packageVo = packageList.get(i);
        	String packageCode = packageVo.getPackageCode(); //服务包编码
        	Map<String, Object> couponParams = new HashMap<String, Object>();
        	//卡券查询参数
        	couponParams.put("packageCode", packageCode);
        	couponParams.put("checkStatus", Arrays.asList("0", "1", "4"));
        	//卡券更新参数
        	couponParams.put("updateTime", executeTime);
        	couponParams.put("status", "2");
        	couponService.updateByParam(couponParams);
        	Map<String, Object> update_package_params = new HashMap<String, Object>();
        	update_package_params.put("packageCode", packageCode);
        	update_package_params.put("updateTime", executeTime);
        	update_package_params.put("status", "2"); //已失效
        	packageService.updateByParam(update_package_params);
        }
        System.out.println("------------服务包变更完毕------------");
        Map<String, Object> search_card_params = new HashMap<String, Object>();
        search_card_params.put("expDate", now);
        search_card_params.put("status", "2");
        List<StoreCouponEntity> cardList = couponService.findListByParam(search_card_params);
        for(int i=0; i<cardList.size(); i++){
        	StoreCouponEntity cardEntity = cardList.get(i);
        	String couponNum = cardEntity.getCouponNum();
        	Map<String, Object> couponParams = new HashMap<String, Object>();
        	Date executeTime = new Date();
        	//卡券查询参数
        	couponParams.put("couponNum", couponNum);
        	couponParams.put("checkStatus", Arrays.asList("0", "1", "4"));
        	//卡券更新参数
        	couponParams.put("updateTime", executeTime);
        	couponParams.put("status", "2");
        	couponService.updateByParam(couponParams);
        }
        System.out.println("------------卡券变更完毕------------");
    }
	
	
	/**
	 * 
	 * @author R
	 * 2021年12月28日下午2:19:55
	 * @desc 变更卡券状态 
	 * 状态变更规则：
	 * 1.更新卡券生效状态  由0-待生效 更新为1-已生效
	 */
	@Scheduled(cron = "0 5 0 * * ?")    //每天凌晨零点5分执行
    public void changeCouponStatus(){
		System.out.println("------------卡券生效变更开始------------");
        Map<String, Object> search_card_params = new HashMap<String, Object>();
        String effDate = DateUtil.DateToString(new Date(), "yyyy-MM-dd");
        search_card_params.put("effDate", effDate+" 00:00:00");
        search_card_params.put("status", "0");
        List<StoreCouponEntity> cardList = couponService.findCouponInfo(search_card_params);
        for(int i=0; i<cardList.size(); i++){
        	StoreCouponEntity cardEntity = cardList.get(i);
        	String couponNum = cardEntity.getCouponNum();
        	Map<String, Object> couponParams = new HashMap<String, Object>();
        	Date executeTime = new Date();
        	//卡券查询参数
        	couponParams.put("couponNum", couponNum);
        	couponParams.put("checkStatus", Arrays.asList("0"));
        	//卡券更新参数
        	couponParams.put("updateTime", executeTime);
        	couponParams.put("status", "1");
        	couponService.updateByParam(couponParams);
        }
        System.out.println("------------卡券生效变更完毕------------");
    }
	
}
